# -*- coding: utf8 -*-

__author__ = 'sergey'

import gobject
from datetime import datetime
from threading import Thread, Event
from time import sleep

class ProcessArchives(Thread):
    """
    __init__ : args = ( app, ui, db_req, stats_req )
    """

    app = None
    ui = None
    db_req = None
    stats_req = None

    stop_event = None

    done = False

    log = None

    def __init__(self, group=None, target=None, name=None,
                 args=(), kwargs=None, verbose=None):
        self.app, self.ui, self.db_req, self.stats_req = args
        Thread.__init__(self, group, target, name, args, kwargs, verbose)

    def run(self):
        from logger import get_file_logger
        self.log = get_file_logger("worker-" + self.__class__.__name__)
        self.stop_event = Event()
        try:
            self.process_archives()
        except Exception as e:
            self.log.error(e)
        self.db_req.close()
        return 0


    def update_ui(self):
        self.app.update_ui_stats_archives()
        return not self.done


    def process_archives(self):

        self.log.info("START")

        self.app._timed_stats_begin()

        from archive import get_supported_mimes, is_supported

        self.db_req.clean_image_vector10()
        self.db_req.clean_image_vector_big()
        self.db_req.clean_image_vector_numpy()
        self.db_req.clean_image_vector_bitstr()
        self.db_req.clean_image_vector_gray()
        self.db_req.clean_image2image()
        self.db_req.clean_file2file()
        self.db_req.clean_image()

        mimes = get_supported_mimes()
        self.log.debug("Mime types: %r" % (mimes,))

        count_archives = self.db_req.get_files_count_by_mimes(mimes)
        self.log.debug("Count archives: %r" % (count_archives,))

        self.done = False

        self.stats_req.set("errors_count", 0)
        self.stats_req.set("archives_count", 0)
        self.stats_req.set("archives_size", 0)
        self.stats_req.set("archives_processed", 0)
        self.stats_req.set("archives_processed_size", 0)
        self.stats_req.set("images_count", 0)
        self.stats_req.set("images_processed", 0)

        if not count_archives:
            self.done = True
            self.stop_event.clear()
            self.ui.get_object("button_process_archives").set_active(False)

            self.app.clean_progress_archive_ui()
            self.log.info("STOP")

            return

        gobject.timeout_add(200, self.update_ui)

        task_manager = self.app.getTaskManager()
        from service.process_archive import ProcessArchiveDirectRequest

        archive_req = task_manager.get_request().getProcessArchive()
        while not archive_req.ping():
            sleep(0.1)
        addr = archive_req.get_address()
        archive_req = ProcessArchiveDirectRequest(addr)


        limit = 100
        offset= 0


        self.log.debug("Get offset=%d, count=%d" % (offset, limit,))
        t1 = datetime.now()
        files = list(self.db_req.get_files_by_mimes(mimes, offset, limit))
        t2 = datetime.now()
        self.log.debug("Got %d in %s" % (len(files), t2 - t1,))

        while not self.stop_event.isSet():

            try:
                file_item = files.pop()
            except:
                file_item = None

            if not file_item:

                if offset+limit >= count_archives:
                    break
                offset += limit
                self.log.debug("Get offset=%d, count=%d" % (offset, limit,))
                t1 = datetime.now()
                files = list(self.db_req.get_files_by_mimes(mimes, offset, limit))
                t2 = datetime.now()
                self.log.debug("Got %d in %s" % (len(files), t2 - t1,))
                continue

            if is_supported(file_item["mime"]):
                self.stats_req.inc("archives_count", 1)
                self.stats_req.inc("archives_size", file_item["size"])
                archive_req.process(
                    file_item["id"],
                    self.db_req.get_file_full_path(file_item),
                    file_item["mime"],
                    file_item["size"]
                )

        self.log.info("Sent all archives to process")
        self.log.info("WAITING")

        while True:
            stats = self.stats_req.get_update()
            if (stats["archives_count"] > 0) and (stats["archives_processed"] != stats["archives_count"]):
                sleep(0.1)
            else:
                break
            if self.stop_event.isSet():
                break

        self.log.info("ALL DONE")
        self.done = True

        archive_req.quit()

        if not self.stop_event.isSet():
            self.ui.get_object("button_process_archives").set_active(False)

        self.stop_event.clear()

        self.app.clean_progress_archive_ui()
        self.log.info("STOP")

        return
